//
//  ViewController4.m
//  测试
//
//  Created by 李李善 on 16/7/30.
//  Copyright © 2016年 李李善. All rights reserved.
//

#import "BaseViewController.h"
#import <objc/runtime.h>
@interface BaseViewController ()<UIGestureRecognizerDelegate>
{
    BOOL _isLoad;
}
@end

@implementation BaseViewController

- (void)dealloc
{
    
    NSLog(@"%@ 这个类被强暴了",NSStringFromClass([self class]));
    //移除通知
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}



- (void)viewWillAppear:(BOOL)animated{//数据价加载
    [super viewWillAppear:animated];
  
//    if ([self respondsToSelector:@selector(gestureRecognizerShouldBegin:)]) {
//        NSLog(@"asd");
//    }
    
    if (NO==_isLoad) {
        
      
        WSELF(weakSelf);
        dispatch_async(dispatch_get_main_queue(),^{
            [weakSelf onLoadDataForPageNum:1];
        });
        
        [self onLoadViewByWillAppear:self.view];
        
    }
}



-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
   // if (NO==_isLoad) {
        
        CGRect frame = self.view.frame;
        if (self.layoutBlock) {
            self.layoutBlock(self.view,frame.origin,frame.size,self.view.center);
        }
//    }
    
}




- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];//执行行为，比如运行动画效果。
    
    if (NO==_isLoad) {
        _isLoad = YES;
        if(self.loadBlock)
        {
            self.loadBlock();
        }
        [self onLoadAnimatedByDidAppear:self.model];
    }
}


- (void)viewDidLoad {//自定义视图。
    [super viewDidLoad];
    
    self.view.opaque = YES;
    
    self.view.backgroundColor = [UIColor whiteColor];
    

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        [self.navigationController.interactivePopGestureRecognizer setEnabled:YES];
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }

//    self.edgesForExtendedLayout = UIRectEdgeNone;
//    不让控制器自动生成64的偏移
//    self.automaticallyAdjustsScrollViewInsets = NO;
    

    _isLoad = NO;
    
    
    
}
//-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
//{
//    if (self.childViewControllers.count==1) {
//        return NO;
//    }else
//    {
//        return YES;
//    }
//}


- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

@end









@interface BaseTableViewController ()<UIGestureRecognizerDelegate>
{
    BOOL _isLoad;
}
@end

@implementation BaseTableViewController

- (void)dealloc
{
    NSLog(@"%@ 这个类被强暴了",NSStringFromClass([self class]));
    //移除通知
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
    
}



//-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
//{
//    if (self.childViewControllers.count==1) {
//        return NO;
//    }else
//    {
//        return YES;
//    }
//}


- (void)viewWillAppear:(BOOL)animated{//更新视图信息。
    
    [super viewWillAppear:animated];
    
    if (NO==_isLoad) {
        
        WSELF(weakSelf);
        dispatch_async(dispatch_get_main_queue(),^{
            [weakSelf onLoadDataForPageNum:1];
        });
        [self onLoadViewByWillAppear:self.view];
    }
  
}
-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
//    if (NO==_isLoad) {
    
        CGRect frame = self.view.frame;
        if (self.layoutBlock) {
            self.layoutBlock(self.view,frame.origin,frame.size,self.view.center);
//        }
    }
    
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];//执行行为，比如运行动画效果。
    if (NO==_isLoad) {
        _isLoad = YES;
        if(self.loadBlock)
        {
            self.loadBlock();
        }
       [self onLoadAnimatedByDidAppear:self.model];
        
    }
}

- (void)viewDidLoad {//自定义视图。
    [super viewDidLoad];
    
    self.view.opaque = YES;
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        [self.navigationController.interactivePopGestureRecognizer setEnabled:YES];
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
    
    _isLoad = NO;
    
    //self.automaticallyAdjustsScrollViewInsets = NO;

}

@end








@interface BaseCollectionViewController ()<UIGestureRecognizerDelegate>
{
    BOOL _isLoad;
}
@end
@implementation BaseCollectionViewController

- (void)dealloc
{
    NSLog(@"%@ 这个类被强暴了",NSStringFromClass([self class]));
    //移除通知
    [[NSNotificationCenter defaultCenter]removeObserver:self];

}

//-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
//{
//    if (self.childViewControllers.count==1) {
//        return NO;
//    }else
//    {
//        return YES;
//    }
//}

- (void)viewWillAppear:(BOOL)animated{//更新视图信息。
    
    [super viewWillAppear:animated];
    if (NO==_isLoad) {
    
        WSELF(weakSelf);
        dispatch_async(dispatch_get_main_queue(),^{
            [weakSelf onLoadDataForPageNum:1];
        });
        [self onLoadViewByWillAppear:self.view];
    }
    
}



- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];//执行行为，比如运行动画效果。
    if (NO==_isLoad) {
        _isLoad = YES;
        if(self.loadBlock)
        {
            self.loadBlock();
        }
        
        [self onLoadAnimatedByDidAppear:self.model];
    }
}


-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
//    if (NO==_isLoad) {
    
        CGRect frame = self.view.frame;
        if (self.layoutBlock) {
            self.layoutBlock(self.view,frame.origin,frame.size,self.view.center);
        }
//    }
    
}




- (void)viewDidLoad {//自定义视图。
    
    [super viewDidLoad];
    
    self.view.opaque = YES;
    
    self.view.backgroundColor = [UIColor whiteColor];
   
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        [self.navigationController.interactivePopGestureRecognizer setEnabled:YES];
        self.navigationController.interactivePopGestureRecognizer.delegate = self;
    }
    
    
    
    _isLoad = NO;
    
    
}



@end


@implementation UIViewController (rootVC)
+(UIViewController *)rootViewController
{
    return [UIApplication sharedApplication].keyWindow.rootViewController;
}

+(UIWindow *)appWindow
{
    return [UIApplication sharedApplication].keyWindow;
}
+(id)appDelegate
{
    return (id)[UIApplication sharedApplication].delegate;
}
@end



static char * layout_Block = "layoutBlock";
static char * load_Block = "loadBlock";
@implementation UIViewController (selector)

-(CGFloat)navBarHeight
{
   CGRect r = self.navigationController.navigationBar.frame;
   return r.origin.y+r.size.height;
}

-(layoutBlock)layoutBlock
{
    return objc_getAssociatedObject(self, layout_Block);

}
-(void)setLayoutBlock:(layoutBlock)layoutBlock
{
      objc_setAssociatedObject(self, layout_Block, layoutBlock,OBJC_ASSOCIATION_COPY_NONATOMIC);
}

-(loadAnimatedBlock)loadBlock
{
      return objc_getAssociatedObject(self, load_Block);
}
-(void)setLoadBlock:(loadAnimatedBlock)loadBlock
{
        objc_setAssociatedObject(self, load_Block, loadBlock,OBJC_ASSOCIATION_COPY_NONATOMIC);
}

/** < 加载动画 > */
-(void)onLoadAnimatedByDidAppear:(id)model{
    
}

/** < 加载视图 > */
-(void)onLoadViewByWillAppear:(UIView *)view{
    
}

/** < 网络请求 > */
-(void)onLoadDataForPageNum:(NSInteger)pageNum{
    
    
}


@end
